home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / i / internet / software / tuwtcpsr / timer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-06  |  3.0 KB  |  158 lines

  1. #include <time.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "pktdrv.h"
  5. #include "timer.h"
  6. #include "mbuf.h"
  7.  
  8. #include "nettrace.h"
  9. #include "cookie.h"
  10.  
  11. static char str[200];
  12.  
  13. #define TM_FREE        (0L)
  14. #define TM_STOP        ((clock_t)1)
  15.  
  16. typedef struct
  17. {
  18.     clock_t    tm_timeout;
  19.     void    (*tm_handler)(TIMER);
  20. } TM_ENTRY;
  21.  
  22. #define TM_MINCNT    8
  23.  
  24. static TM_ENTRY *tm_table = NULL;
  25. static int        tm_count = 0;
  26. static int        tm_active = 0;
  27.  
  28. TIMER tm_alloc(void)
  29. {
  30. int i;
  31. TM_ENTRY *tm;
  32.  
  33.     if(!tm_table)
  34.     {
  35.         tm_table = (TM_ENTRY *)getmem(sizeof(TM_ENTRY) * TM_MINCNT);
  36.         if(!tm_table) return(-1);
  37.         tm_count = TM_MINCNT;
  38.         tm = tm_table;
  39.         for(i=0;i<TM_MINCNT;i++)
  40.         {            /* init table */
  41.             tm->tm_timeout = TM_FREE;
  42.             tm->tm_handler = NULL;
  43.             tm++;
  44.         }
  45.         net_demux(TRUE,tm_handler);
  46.     }
  47.     tm = tm_table;
  48.     for(i=0;i<tm_count; i++)
  49.     {
  50.         if(tm->tm_timeout == TM_FREE)
  51.         {
  52.             tm->tm_timeout = TM_STOP;
  53.             tm->tm_handler = NULL;
  54.             tm_active++;
  55.             return(i);
  56.         }
  57.     }
  58.             /* no free timerentry, enlarge table */
  59.     tm = (TM_ENTRY *)resizemem(tm_table,
  60.                                 (TM_MINCNT+tm_count)*sizeof(TM_ENTRY));
  61.     if(!tm)
  62.     {
  63.         resizemem(tm_table,tm_count*sizeof(TM_ENTRY));
  64.         return(-1);
  65.     }
  66.     tm_table = tm;
  67.     tm += tm_count+1;
  68.     for(i=1;i<TM_MINCNT;i++)
  69.     {            /* init table */
  70.         tm->tm_timeout = TM_FREE;
  71.         tm->tm_handler = NULL;
  72.         tm++;
  73.     }
  74.     i = tm_count;
  75.     tm_table[i].tm_timeout = TM_STOP;
  76.     tm_table[i].tm_handler = NULL;
  77.     tm_count += TM_MINCNT;
  78.     tm_active++;
  79.     return(i);
  80. }
  81.  
  82.  
  83. int tm_set(u_long tm_timeout, void(*tm_handler)(),TIMER tm)
  84. {
  85. #ifdef DEBUG
  86. sprintf(str,"~set tm %d\n",tm);
  87. TRACE(str);
  88. #endif
  89.     if(tm < 0 ||
  90.        !tm_table ||
  91.        tm >= tm_count ||
  92.        tm_table[tm].tm_timeout == TM_FREE) return(-1);
  93.        
  94.     tm_table[tm].tm_timeout = clock() + (clock_t)tm_timeout;
  95.     if(tm_handler)
  96.         tm_table[tm].tm_handler = tm_handler;
  97.     return(tm);
  98. }
  99.  
  100. int tm_stop(TIMER tm)
  101. {
  102. #ifdef DEBUG
  103. sprintf(str,"~stop tm %d\n",tm);
  104. TRACE(str);
  105. #endif
  106.     if(tm<0 || !tm_table || tm>=tm_count) return(-1);
  107.     if(tm_table[tm].tm_timeout == TM_FREE) return(-1);
  108.     tm_table[tm].tm_timeout = TM_STOP;
  109.     return(tm);
  110. }
  111.  
  112.  
  113. int tm_free(TIMER tm)
  114. {
  115. #ifdef DEBUG
  116. sprintf(str,"~free tm %d\n",tm);
  117. TRACE(str);
  118. #endif
  119.     if(tm<0 || !tm_table || tm >= tm_count) return(-1);
  120.     if(tm_table[tm].tm_timeout == TM_FREE) return(-1);
  121.     tm_table[tm].tm_timeout = TM_FREE;
  122.     if(tm_active > 0) tm_active--;
  123.     if(!tm_active)
  124.     {
  125.         net_demux(FALSE,tm_handler);
  126.         freemem(tm_table);
  127.         tm_table = NULL;
  128.     }
  129.     return(tm);
  130. }
  131.  
  132. int tm_handler(void)
  133. {
  134. u_long timeout;
  135. int i;
  136. TM_ENTRY *tm;
  137.     if(!tm_table) return(-1);
  138.     timeout = clock();
  139.     tm = tm_table;
  140.     for(i=0; i<tm_count; i++)
  141.     {
  142.         if(tm->tm_timeout > TM_STOP)
  143.         {
  144.             if(tm->tm_timeout <= timeout &&
  145.                tm->tm_handler)
  146.             {
  147. #ifdef DEBUG
  148. sprintf(str,"~tmhandler %d: time = %lu, timeout = %lu\n",i,timeout, tm->tm_timeout);
  149. TRACE(str);
  150. #endif
  151.                 tm->tm_timeout = TM_STOP;
  152.                      tm->tm_handler(i);
  153.             }
  154.         }
  155.         tm++;
  156.     }
  157.     return(TRUE);
  158. }